﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Windows.Foundation;

namespace SoapClient
{
    public sealed class LengthUnits
    {
        LengthUnitService.lengthUnitSoapClient client;
        public LengthUnits()
        {
            client = new LengthUnitService.lengthUnitSoapClient();
        }
        public IAsyncOperation<double> GetAsync(double number,
            LengthUnitService.Lengths from, LengthUnitService.Lengths to)
        {
            return client.ChangeLengthUnitAsync(number, from, to).AsAsyncOperation();
        }

        public IAsyncOperationWithProgress<double, string> GetWithProgressAsync(double number,
            LengthUnitService.Lengths from, LengthUnitService.Lengths to)
        {
            return AsyncInfo.Run(async delegate(CancellationToken cancellationToken,
                IProgress<string> progress)
            {
                double result;
                progress.Report(client.State.ToString());
                await client.OpenAsync();
                progress.Report(client.State.ToString());
                cancellationToken.ThrowIfCancellationRequested();
                result = await client.ChangeLengthUnitAsync(number, from, to);
                progress.Report(client.State.ToString());
                cancellationToken.ThrowIfCancellationRequested();
                await client.CloseAsync();
                progress.Report(client.State.ToString());
                return result;
            });
        }
    }
}
